package com.twu.biblioteca;

import java.util.Collection;
import java.util.HashMap;

public class Library {
    private HashMap<String, Book> books;
    private HashMap<String, Movie> movies;

    public Library() {
        books = new HashMap<String, Book>();
        movies = new HashMap<String, Movie>();
    }

    public void addDefaultBooks() {
        addBook(new Book("Test-Driven Development", "Kent Beck", 2003));
        addBook(new Book("Head First Java", "Bert Bates and Kathy Sierra", 2004));
        addBook(new Book("Harry Potter and the Philosopher's Stone", "J.K. Rowling", 1997));
    }

    public void addDefaultMovies() {
        addMovie(new Movie("Test-Driven Directing", 2004, "Kent Beck", 6));
        addMovie(new Movie("Jaws 7: Revenge of the Java", 2006, "Oracle Corporation", 3));
        addMovie(new Movie("Harry Potter and the Additional Movie", 1998, "J.K. Rowling", -1));
    }

    public void addBook(Book book) {
        books.put(book.getTitle(), book);
    }

    public void addMovie(Movie movie) {
        movies.put(movie.getTitle(), movie);
    }

    public Book findBook(String title) {
        return books.get(title);
    }

    public Movie findMovie(String title) {
        return movies.get(title);
    }

    public boolean checkInItem(LibraryItem item, User user) {
        if (item.isCheckedOut() && item.getCheckedOutTo() == user) {
            item.setCheckedOutTo(null);
            return true;
        } else {
            return false;
        }
    }

    public boolean checkOutItem(LibraryItem item, User user) {
        if (user == null) {
            throw new IllegalArgumentException("Must provide a user when checking out an item.");
        }

        if (!item.isCheckedOut()) {
            item.setCheckedOutTo(user);
            return true;
        } else {
            return false;
        }
    }

    public Collection<Book> getBooks() {
        return books.values();
    }

    public Collection<Movie> getMovies() {
        return movies.values();
    }
}
